import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { NzModalService, NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { STData } from '@delon/abc';
import { ApiService } from '@core/services/api';
import { environment } from '@env/environment';
import { DoorProductorDic, Operator } from '@core/config/dic.config';

@Component({
  selector: 'app-audit-list-module',
  templateUrl: './audit-list.component.html',
  styleUrls: ['./index.less'],
})
export class AuditListModuleComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  imgUrl = environment.SERVER_URL + '/';
  loading = false;
  // 审核描述内容
  textareaData: string;
  selectData: any[];
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '多选',
        type: ColumnType.checkbox,
      },
      {
        index: 'deviceIp',
        title: '点位IP',
        filter: {
          placeholder: '请输入点位IP',
          type: 'string',
        },
      },
      {
        index: 'deviceName',
        title: '设备名称',
        filter: {
          placeholder: '请输入设备名称',
          type: 'string',
        },
      },
      {
        index: 'areaName',
        title: '所属村',
        /*filter: {
          placeholder: '请输入村庄',
          type: 'string',
        },*/
      },
      {
        index: 'houseAddr',
        title: '楼栋地址',
        filter: {
          placeholder: '请输入楼栋',
          type: 'string',
        },
      },
      {
        index: 'doorName',
        title: '门名称',
        /*filter: {
          placeholder: '请输入门名称',
          type: 'string',
        },*/
      },
      {
        index: 'facCode',
        title: '厂家',
        filter: {
          placeholder: '请选择厂家',
          type: 'select',
          hideSearch: true,
          items: DoorProductorDic
        },
      },
      {
        index: 'deviceCode',
        title: '设备ID',
        filter: {
          placeholder: '请输入设备ID',
          type: 'string',
        },
      },
      {
        index: 'deviceOperator',
        title: '所属运营商',
        filter: {
          placeholder: '请选择运营商',
          type: 'select',
          items: Operator
        },
      },
      {
        index: 'offTime',
        title: '离线时间',
        /*filter: {
          placeholder: '离线时间',
          type: 'string',
        },*/
      },
      {
        index: 'offHour',
        title: '离线总时长',
        /*filter: {
          placeholder: '离线总时长',
          type: 'string',
        },*/
      },
      {
        index: 'auditStatus',
        title: '审核状态',
        type: ColumnType.tag,
        tag: {
          0: { text: '未申请', color: 'grey'},
          1: { text: '待审核', color: 'red' },
          2: { text: '审核通过', color: 'green' },
          3: { text: '审核不通过', color: 'red' },
        },
        filter: {
          type: 'select',
          items: [
            { label: '未申请', value: '0'},
            {label: '待审核', value: '1'},
            {label: '审核通过', value: '2'},
            {label: '审核不通过', value: '3'},
          ]
        }
      },
      {
        index: 'remark',
        title: '描述',
        /*filter: {
          placeholder: '描述',
          type: 'string',
        },*/
      },
    ],
    editBtn: [
      { title: '查看附件' ,
      },
      { title: '审核',
        iif: (data: STData) => {
        return data.auditStatus === 1
        } },
    ],
    showCheckBox: true,
    reqUrl: this.api.device.offLinePage.url,
  };
  // process(data: STData[]): STData[] {
  //   return data.map(item => {
  //      item .disabled = item.shzt === '0' || item.shzt === '1';
  //      console.log(item);
  //      return item;
  //   })
  // }
  isVisible = false;
  fileList: any = [];
  previewImage: string | undefined = '';
  previewVisible = false;
  // 创建审核模块方法
   myModal: NzModalRef;
  createModal(element: TemplateRef<{}>) {
    this.myModal = this.modal.create({
      nzTitle: '审核',
      nzContent: element,
      nzFooter: null,
    });
  }

  auditClick(val: number) {
    if (!this.textareaData) {
      this.message.error('请输入描述!');
      return;
    }
    this.loading = true;
    const body = {
      auditStatus: val,
      id: this.selectData[0].id,
      remark: this.textareaData
    }
    this.http.request<any>(this.api.assess.applyReduction.method, this.api.assess.applyReduction.url, {body}).subscribe(res => {
      if (res.status=== '200') {
        this.message.success('审核完成');
        this.myModal.close();
        this.st.reload();
      }
    })
  }

  // 操作区
  operate(e, element: TemplateRef<{}>): void {
    if (e.title === '查看附件') {
      if (!!!e.data.enclosure) {
        this.message.error('没有附件信息')
        return;
      }
      this.fileList = e.data.enclosure.split(';');
      this.fileList.pop();
      this.isVisible = true;
    } else if (e.title === '审核') {
      if (e.data.status === 1) {
        this.selectData = [];
        this.selectData[0] = e.data;
        this.createModal(element);
      }
    }

  }

  // 选择数据
  selectChange(e) {
    console.log(e)
    this.selectData = e;
  }

  // 根据状态检索不需要被全选的数据
  dataProcess(data: STData[]) {
    return data.map((i: STData, index: number) => {
      i.disabled = i.auditStatus === 2 || i.auditStatus === 3;
      return i;
    });
  }

  // 批量审核
  batchAudit(el: TemplateRef<{}>) {
    if (!!this.selectData && this.selectData.length !== 0) {
      this.modal.create({
        nzTitle: '审核',
        nzContent: el,
        nzFooter: null,
      });

    } else {
      this.message.warning('请选择数据');
    }
  }

  constructor(
    private http: _HttpClient,
    private modal: NzModalService,
    private message: NzMessageService,
    private api: ApiService
  ) {
  }

  ngOnInit() {
  }


}
